package com.antd.auth.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.antd.admin.api.entity.SysUser;
import com.antd.auth.fegin.RemoteUserService;
import com.antd.common.auth.security.AntdUserDetails;
import com.antd.common.core.util.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Set;

/**
 * TODO
 *
 * @author 子丶沫
 * @version 1.0
 * @date 2020/11/19 10:05
 */
@Slf4j
@Component
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        R<SysUser> data = remoteUserService.findByUsername(username);
        if (data.getCode() == HttpStatus.HTTP_NOT_FOUND) {
            log.debug("系统异常,请稍后再试！");
            throw new UsernameNotFoundException("系统异常,请稍后再试！");
        }
        if(ObjectUtil.isNull(data.getData())||data.getCode() == HttpStatus.HTTP_INTERNAL_ERROR){
            throw new UsernameNotFoundException("用户：" + username + " 不存在.");
        }
        SysUser user = data.getData();
        Set<String> permissions = user.getPermissions();
        Collection<? extends GrantedAuthority> authorityList = getGrantedAuthorityList(permissions);
        return new AntdUserDetails(user.getUserId(), username,user.getPassword(), authorityList,user.getLockFlag());
    }
    /**
     * 手机验证码登录
     *
     * @param mobile
     * @return
     * @throws UsernameNotFoundException
     */
    public UserDetails loadUserByMobile(String mobile) throws UsernameNotFoundException {
        //  通过手机号mobile去数据库里查找用户以及用户权限
        R<SysUser> data = remoteUserService.findByMobile(mobile);
        if (data.getCode() == HttpStatus.HTTP_NOT_FOUND) {
            log.debug("系统异常,请稍后再试！");
            throw new UsernameNotFoundException("系统异常,请稍后再试！");
        }
        if(ObjectUtil.isNull(data.getData())||data.getCode() == HttpStatus.HTTP_INTERNAL_ERROR){
            throw new UsernameNotFoundException("手机号码：" + mobile + " 不存在.");
        }
        SysUser user = data.getData();
        Set<String> permissions = user.getPermissions();
        Collection<? extends GrantedAuthority> authorityList = getGrantedAuthorityList(permissions);
        return new AntdUserDetails(user.getUserId(), user.getUsername(),user.getPassword(), authorityList);    }

        private Collection<? extends GrantedAuthority> getGrantedAuthorityList(Set<String> permissions){
            Collection<? extends GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(permissions.toArray(new String[0]));
            return authorities;
        }
}
